/**
 * Created by user on 2017/12/15.
 */
var originData = {}
import cloneDeep from 'lodash/cloneDeep'
import EventCenter from './EventCenter'
import MessageCenter from './MessageCenter'
import GUID from './GUID'
import Type from './Type'
import GlobalTime from './GlobalTime'
import VoicePlayer from './VoicePlayer'
import VideoPlayer from './VideoPlayer'
import ReplayVideoPlayer from './ReplayVideoPlayer'
import Request from './Request'
const showLog = true
class Data {
  roomId = null
  test = 0
  user = null
  code = null
  get roomId() {
    return this.roomId
  }
  set roomId(id) {
    this.roomId = id
  }
  get test() {
    return this.test
  }
  set test(b) {
    this.test = b
  }
  get user() {
    return this.user
  }
  set user(u) {
    this.user = u
  }
  get code() {
    return this.code
  }
  set code(c) {
    this.code = c
  }
}
const getSeq = () => {
  return GlobalTime.getGlobalTime() - MessageCenter.getStartTime()
}
const getStamp = () => {
  return GlobalTime.getGlobalTime()
}
const setData = (type, data) => {
  var obj = {}
  for (var key in data) {
    obj[key] = data[key]
  }
  originData[type] = obj
}
const setZero = (n) => {
  return n < 10 ? ('0' + n) : n
}
const formatDiff = (diff) => {
  var d, h, m, s, ds = 24 * 60 * 60 * 1000, hs = 60 * 60 * 1000,
     ms = 60 * 1000, ss = 1000
  d = parseInt((diff / ds).toFixed(2))
  diff = diff - d * ds
  h = parseInt((diff / hs).toFixed(2))
  diff = diff - h * hs
  m = parseInt((diff / ms).toFixed(2))
  diff = diff - m * ms
  s = parseInt((diff / ss).toFixed(2))
  d = (d > 0) ? (d + '天 ') : ''
  h = setZero(h)
  m = setZero(m)
  s = setZero(s)
  return d + h + ':' + m + ':' + s
}
const cloneObj = (obj) => {
  var o;
  if (typeof obj === "object") {
    if(obj === null){
      o = null;
    }else{
      if(obj instanceof Array){
        o = [];
        for(var i = 0, len = obj.length; i < len; i++){
          o.push(cloneObj(obj[i]));
        }
      }else{
        o = {};
        for(var k in obj){
          o[k] = cloneObj(obj[k]);
        }
      }
    }
  }else{
    o = obj;
  }
  return o;
}
const formatSocketMsg = (msg) => {
  var m = null
  var type_obj = checkMsgType(msg)
  if (type_obj) {
    var live_time = msg.live_time || {}
    m = {
      type: type_obj.type,
      timestamp: parseInt(live_time.timestamp) || 0,
      seq_time: parseInt(live_time.seq_time) || 0,
      isTeacher: msg.from_info && msg.from_info.isTeacher || 0,
      sender: msg.from_info || null,
      data: msg.data || null,
      remoteMsgId: msg.msg_id
    }
    if (Type.DELAY_TYPE.indexOf(type_obj.type) >= 0) {
      m.isDelay = true
    }
  }
  return m
}
const checkMsgType = (msg) => {
  if (!msg) {
    return null
  }
  var current = null
  for (var key in Type.GET_TYPE) {
    var t = Type.GET_TYPE[key]
    if (t.msg_type === msg.msg_type && msg.msg_code === t.msg_code) {
      current = t
      break
    }
  }
  return current
}

function LogStr() {
  if (showLog) {
    console.log.apply(console, arguments);
  }
}
const formatTime = (timestamp, type) => {
  var y, m, d, h, mm, s
  var t = new Date(timestamp)
  y = t.getFullYear()
  m = setZero(t.getMonth() + 1)
  d = setZero(t.getDate())
  h = setZero(t.getHours())
  mm = setZero(t.getMinutes())
  s = setZero(t.getSeconds())
  if (type === 'line') {
    return y + '-' + m + '-' + d + ' ' + h + ':' + mm
  } else if (type === 'point') {
    return y + '.' + m + '.' + d + ' ' + h + ':' + mm
  }
}

const singleClone = _ => JSON.parse(JSON.stringify(_))

const singleSort = (arr, key) => {
  let _ = singleClone(arr)
  for (let i = 0; i < _.length - 1; i++) {
    for (let j = 0; j < _.length - i - 1; j++) {
      if (parseInt(_[j][key]) < parseInt(_[j + 1][key])) {
        let tmp = singleClone(_[j])
        let tmp1 = singleClone(_[j + 1])
        _[j] = tmp1
        _[j + 1] = tmp
      }
    }
  }
  return _
}
function getEleInnerHtml(id) {
  return document.getElementById(id) && document.getElementById(id).innerHTML || ''
}

module.exports = {
  EventCenter,
  GUID,
  Type,
  formatDiff,
  cloneObj,
  getSeq,
  getStamp,
  Data,
  formatSocketMsg,
  LogStr,
  formatTime,
  MessageCenter,
  VoicePlayer,
  VideoPlayer,
  ReplayVideoPlayer,
  Request,
  setData,
  originData,
  cloneDeep,
  singleClone,
  singleSort,
  setZero,
  getEleInnerHtml
}
